package com.menu.Controller;

import com.menu.Pojo.SecurityUser;
import com.menu.Pojo.User;
import com.menu.Service.UserService;
import com.menu.Utils.CommonUtil;
import com.menu.Utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "个人中心")
@RequestMapping("/User")
@RestController
public class PersonController {

    @Autowired
    private UserService userService;
    @Autowired
    private CommonUtil commonUtil;

    @ApiOperation("更改头像")
    @PutMapping("/modifyHeadImg")
    public ResponseResult modifyHeadImg(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser,@RequestPart("multipartFile") MultipartFile multipartFile){
        if (multipartFile == null || multipartFile.getSize() == 0){
            return ResponseResult.error("图片上传有误！");
        }
        Integer id = securityUser.getUserId();
        return userService.modifyHeadImg(id,multipartFile);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    public ResponseResult getUserInfo(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        return userService.getUserInfoById(securityUser.getUserId());
    }

    @ApiOperation("更改用户信息")
    @PutMapping("/modifyUser")
    public ResponseResult modifyUser(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser,
                                                 User user){
        String address = user.getAddress();
        if (address != null && !commonUtil.verifyStirng(address)){
            return ResponseResult.error("地址信息有误！");
        }
        String like = user.getLike();
        if (like != null && !commonUtil.verifyStirng(like)){
            return ResponseResult.error("用户喜欢有误！");
        }
        String nikeName = user.getNickname();
        if (nikeName != null && !commonUtil.verifyStirng(nikeName)){
            return ResponseResult.error("用户昵称有误！");
        }
        String realName = user.getRealName();
        if (realName != null && !commonUtil.verifyStirng(realName)){
            return ResponseResult.error("用户真实姓名有误！");
        }
        Integer sex = user.getSex();
        if (sex != null && !(sex == 1 || sex == 0)){
            return ResponseResult.error("用户性别有误！");
        }
        user.setUserId(securityUser.getUserId());
        return userService.modifyUser(user);
    }

    @ApiOperation("用户修改绑定的邮箱")
    @PutMapping("/modifyEmail")
    public ResponseResult modifyEmail(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser,
                                      String email,String code){
        if (email == null || !email.matches(CommonUtil.email)) {
            return ResponseResult.error("邮箱格式有误！");
        }
        if (code == null || code.length() != 6) {
            return ResponseResult.error("验证码有误！");
        }
        return userService.modifyEmail(securityUser.getUserId(),email,code);
    }

    @ApiOperation("修改密码")
    @PutMapping("/modifyPassword")
    public ResponseResult modifyPassword(@ApiIgnore @AuthenticationPrincipal  SecurityUser securityUser, String oldPassword, String newPassword) {
        if (oldPassword == null || oldPassword.length() < 6 || oldPassword.length() > 16) {
            return ResponseResult.error("旧密码不合法！");
        }
        if (newPassword == null || newPassword.length() < 6 || newPassword.length() > 16) {
            return ResponseResult.error("新密码不合法！");
        }
        if (oldPassword.equals(newPassword)) {
            return ResponseResult.error("新旧密码相同！");
        }
        return userService.modifyPassword(securityUser.getUserId(), oldPassword, newPassword);
    }


}
